#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<string.h>
void sh1(char *command)
{
	char buffer[100];
	char item[10][20];
	char *word;
	char *argv[5];
	strcpy(buffer,command);
	word=strtok(buffer," ");
	int i=0,j;
	while(word!=NULL){
		strcpy(item[i++],word);
		word=strtok(NULL," ");
	}
	for(j=0;j<i;++j){
		argv[j]=(char *)malloc(sizeof(char)*20);
		strcpy(argv[j],item[j]);
	}
	argv[j]=NULL;
	if(strcmp(argv[0],"cd")==0){
		chdir(argv[1]);
	}
	else if(strcmp(argv[0],"pwd")==0){
		char dir[100];
		getcwd(dir,sizeof(dir));
		printf("%s\n",dir);
	}
	else if(strcmp(argv[0],"exit")==0){
		exit(0);
	}
	else{
		pid_t pid;
		pid=fork();
		if(pid==0){
			int error=execvp(argv[0],argv);
			if(error<0){
				perror("execvp");
			}
			exit(127);
		}
		wait(NULL);
	}
	for(j=0;j<i;++j){
		free(argv[j]);
	}
}
int main()
{
    char line[100];
    int c;
    while(1){
    	printf(">");
<<<<<<< HEAD
	fflush(stdout);
	 fgets(line, sizeof(line), stdin);  
=======
    	fflush(stdout);
	    fgets(line, sizeof(line), stdin);  
>>>>>>> e8731d6cb6f17985e2fbaa4ae64307dc9c21fcf2
		line[strlen(line)-1]='\0';  
		sh1(line);            
	}
    return 0;
}
